package com.lda.pruebawidget;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

public class ExampleAppWidgetProvider extends AppWidgetProvider {

	private static final String TAG = "ExampleAppWidgetProvider";

	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		final int N = appWidgetIds.length;

		// Perform this loop procedure for each App Widget that belongs to this
		// provider
		for (int i = 0; i < N; i++) {
			int appWidgetId = appWidgetIds[i];

			// Create an Intent to launch ExampleActivity
			Intent intent = new Intent(context, MainActivity.class);
			PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

			// Get the layout for the App Widget and attach an on-click listener
			// to the button
			RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
			views.setOnClickPendingIntent(R.id.button1, pendingIntent);

			// Tell the AppWidgetManager to perform an update on the current app
			// widget
			appWidgetManager.updateAppWidget(appWidgetId, views);
		}
	}

	public static class UpdateService extends Service {
		@Override
		public void onStart(Intent intent, int startId) {
			Log.d("WordWidget.UpdateService", "onStart()");

			// Build the widget update for today
			RemoteViews updateViews = buildUpdate(this);
			Log.d("WordWidget.UpdateService", "update built");

			// Push update for this widget to the home screen
			ComponentName thisWidget = new ComponentName(this, ExampleAppWidgetProvider.class);
			AppWidgetManager manager = AppWidgetManager.getInstance(this);
			manager.updateAppWidget(thisWidget, updateViews);
			Log.d("WordWidget.UpdateService", "widget updated");
		}

		@Override
		public IBinder onBind(Intent intent) {
			return null;
		}

		/**
		 * Build a widget update to show the current Wiktionary
		 * "Word of the day." Will block until the online API returns.
		 */
		public RemoteViews buildUpdate(Context context) {

			RemoteViews views = null;
			return views;
		}
	}
}
